package _021_10_3

import "strings"

//package main
//import "strings"

//func main() {
//
//}

//解法一
func isPalindrome(s string) bool {
	s = strings.ToUpper(s)
	temp := ""
	for i := 0; i < len(s); i++ {
		if ('A' <= s[i] && 'Z' >= s[i]) || ('0' <= s[i] && '9' >= s[i]) {
			temp += string(s[i])
		}
	}
	left, right := 0, len(temp)-1
	for i := 0; i < len(temp)/2; i++ {
		if temp[left] != temp[right] {
			return false
		}
		left++
		right--
	}

	return true
}

//解法二
func isPalindrome2(s string) bool {
	s = strings.ToUpper(s)

	left, right := 0, len(s)-1

	for left < right {
		for !isalnum(s[left]) && left < right {
			left++
		}
		for !isalnum(s[right]) && left < right {
			right--
		}
		if left < right {
			if s[left] != s[right] {
				return false
			}
			left++
			right--
		}
	}

	return true
}

func isalnum(ch byte) bool {
	return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9')
}
